package com.haojiang.mh.remote.ui.frag

import android.graphics.Color
import android.view.LayoutInflater
import android.view.MotionEvent
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.TextView
import androidx.core.content.ContextCompat
import cn.com.heaton.blelibrary.ble.utils.ThreadUtils
import com.google.android.material.tabs.TabLayout
import com.haojiang.mh.R
import com.haojiang.mh.remote.base.BaseFrag
import com.haojiang.mh.remote.bean.AreaValue
import com.haojiang.mh.remote.enums.EArea
import com.haojiang.mh.remote.enums.EMotor
import com.haojiang.mh.remote.enums.EMotorBtn
import com.haojiang.mh.remote.event.BedImgEvent
import com.haojiang.mh.remote.event.FunChangeEvent
import com.haojiang.mh.remote.ui.dialog.BleSingleDialogFrag
import com.haojiang.mh.remote.utils.BleUtil
import com.haojiang.mh.remote.utils.BleUtil.Companion.inRangeOfView
import com.haojiang.mh.remote.utils.BleUtil.Companion.onActionDown
import com.haojiang.mh.remote.utils.BleUtil.Companion.onActionUp
import com.haojiang.mh.remote.utils.BleUtil.Companion.onTouchListener
import com.haojiang.mh.remote.utils.CmdKey.FOOT_DOWN
import com.haojiang.mh.remote.utils.CmdKey.FOOT_UP
import com.haojiang.mh.remote.utils.CmdKey.HEAD_DOWN
import com.haojiang.mh.remote.utils.CmdKey.HEAD_FOOT_BOTH_DOWN
import com.haojiang.mh.remote.utils.CmdKey.HEAD_FOOT_BOTH_UP
import com.haojiang.mh.remote.utils.CmdKey.HEAD_UP
import com.haojiang.mh.remote.utils.CmdKey.LUMBAR_DOWN
import com.haojiang.mh.remote.utils.CmdKey.LUMBAR_UP
import com.haojiang.mh.remote.utils.CmdKey.MOTOR5_DOWN
import com.haojiang.mh.remote.utils.CmdKey.MOTOR5_UP
import com.haojiang.mh.remote.utils.CmdKey.MOTOR6_DOWN
import com.haojiang.mh.remote.utils.CmdKey.MOTOR6_UP
import com.haojiang.mh.remote.utils.CmdKey.PILLOW_DOWN
import com.haojiang.mh.remote.utils.CmdKey.PILLOW_LUMBAR_BOTH_DOWN
import com.haojiang.mh.remote.utils.CmdKey.PILLOW_LUMBAR_BOTH_TILT_DOWN
import com.haojiang.mh.remote.utils.CmdKey.PILLOW_LUMBAR_BOTH_TILT_UP
import com.haojiang.mh.remote.utils.CmdKey.PILLOW_LUMBAR_BOTH_UP
import com.haojiang.mh.remote.utils.CmdKey.PILLOW_UP
import com.haojiang.mh.remote.utils.MenuUtil.motorMap
import com.haojiang.mh.remote.utils.Util
import com.haojiang.mh.remote.widget.RmText
import com.lx.permission.IPermissionCallback
import com.lx.permission.PermissionManager
import kotlinx.android.synthetic.main.frag_motor.*
import kotlinx.android.synthetic.main.layout_tab.*
import org.greenrobot.eventbus.EventBus
import org.greenrobot.eventbus.Subscribe
import org.greenrobot.eventbus.ThreadMode
import org.jetbrains.anko.textColor


class MotorFrag : BaseFrag() {

    private var cmdList: MutableList<String>? = null
    private var goldenColor: Int = 0
    private var whiteColor: Int = 0

    override fun onLayoutId(): Int {
        return R.layout.frag_motor
    }

    override fun initView() {
        if (!EventBus.getDefault().isRegistered(this)) {
            EventBus.getDefault().register(this)
        }
    }

    override fun initListener() {
        tabLayout.addOnTabSelectedListener(onTabSelectedListener)
        tabLayout.setSelectedTabIndicatorColor(Color.TRANSPARENT)
        goldenColor = ContextCompat.getColor(context, R.color.color_golden_dark_n)
        whiteColor = ContextCompat.getColor(context, R.color.color_white)
    }

    override fun initData() {
        showFrag()
//        showFunction()  //脱机显示控件
    }

    fun traverseMotorView(view: View, list: List<String>) {
        if (view is ViewGroup) {
            for (i in 0 until view.childCount) {
                traverseMotorView(view.getChildAt(i), list)
            }
            return
        }

        if (view is RmText && view.code != null) {
            if (view.tag != null && (view.tag.toString()
                            .equals(EMotorBtn.UP.value, ignoreCase = true) ||
                            view.tag.toString().equals(EMotorBtn.DOWN.value, ignoreCase = true))
            ) {
                view.setOnTouchListener(onMotorTouchListener)
            } else {
                view.visibility = View.GONE
                cmdList?.let {
                    cmdList!!.forEach {
                        if (it.equals(view.code, ignoreCase = true)) {
                            view.visibility = View.VISIBLE
                            view.setOnTouchListener(onTouchListener)
                        }
                    }
                }
            }
        }
    }

    private val mIPermissionCallback = IPermissionCallback {
        BleSingleDialogFrag.show(activity!!)
//        ThreadUtils.asyn {
//            Thread.sleep(800)
//            ThreadUtils.ui {
//                BleSingleDialogFrag.show(activity!!)
//            }
//        }
    }

    private fun callSurface() {
        if (!Util.checkBleStatus(activity!!)) return
        //申请权限
        PermissionManager.getInstance().setIPermissionCallback(mIPermissionCallback, 0)
        PermissionManager.getInstance().requestPermission(
                activity!!,
                BleUtil.permissionArr
        )
    }


    val onMotorTouchListener = object : View.OnTouchListener {

        override fun onTouch(view: View?, event: MotionEvent?): Boolean {
            if (event != null && view != null && view is RmText) {

                when (event.action) {
                    MotionEvent.ACTION_DOWN -> {
                        //如果没有蓝牙设备连接显示蓝牙设备连接界面
//                        if(!BleUtil.isDevConncted()) {
//                            callInterface()
//                            return false
//                        }

                        val tab = tabLayout?.getTabAt(tabLayout!!.selectedTabPosition)
                        val itemViewCode = tab?.tag.toString()
                        val viewTag = view.tag
                        when (itemViewCode.toInt()) {
                            EMotor.HEAD.key -> {
                                when (viewTag) {
                                    EMotorBtn.UP.value -> {
                                        view.code = HEAD_UP
                                        view.img = context.getString(R.string.bed_head)
                                        view.pressType = context.getString(R.string.press_keep)
                                        onActionDown(view)
                                    }
                                    EMotorBtn.DOWN.value -> {
                                        view.code = HEAD_DOWN
                                        view.img = context.getString(R.string.bed_head)
                                        view.pressType = context.getString(R.string.press_keep)
                                        onActionDown(view)
                                    }
                                }
                            }

                            EMotor.FOOT.key -> {
                                when (viewTag) {
                                    EMotorBtn.UP.value -> {
                                        view.code = FOOT_UP
                                        view.img = context.getString(R.string.bed_foot)
                                        view.pressType = context.getString(R.string.press_keep)
                                        onActionDown(view)
                                    }
                                    EMotorBtn.DOWN.value -> {
                                        view.code = FOOT_DOWN
                                        view.img = context.getString(R.string.bed_foot)
                                        view.pressType = context.getString(R.string.press_keep)
                                        onActionDown(view)
                                    }
                                }
                            }

                            EMotor.PILLOW.key -> {
                                when (viewTag) {
                                    EMotorBtn.UP.value -> {
                                        view.code = PILLOW_UP
                                        view.img = context.getString(R.string.bed_pillow)
                                        view.pressType = context.getString(R.string.press_keep)
                                        onActionDown(view)
                                    }
                                    EMotorBtn.DOWN.value -> {
                                        view.code = PILLOW_DOWN
                                        view.img = context.getString(R.string.bed_pillow)
                                        view.pressType = context.getString(R.string.press_keep)
                                        onActionDown(view)
                                    }
                                }
                            }

                            EMotor.LUMBAR.key -> {
                                when (viewTag) {
                                    EMotorBtn.UP.value -> {
                                        view.code = LUMBAR_UP
                                        view.img = context.getString(R.string.bed_lumbar)
                                        view.pressType = context.getString(R.string.press_keep)
                                        onActionDown(view)
                                    }
                                    EMotorBtn.DOWN.value -> {
                                        view.code = LUMBAR_DOWN
                                        view.img = context.getString(R.string.bed_lumbar)
                                        view.pressType = context.getString(R.string.press_keep)
                                        onActionDown(view)
                                    }
                                }
                            }

                            EMotor.MOTOR5.key -> {
                                when (viewTag) {
                                    EMotorBtn.UP.value -> {
                                        view.code = MOTOR5_UP
                                        view.img = context.getString(R.string.bed_all)
                                        view.pressType = context.getString(R.string.press_keep)
                                        onActionDown(view)
                                    }
                                    EMotorBtn.DOWN.value -> {
                                        view.code = MOTOR5_DOWN
                                        view.img = context.getString(R.string.bed_all)
                                        view.pressType = context.getString(R.string.press_keep)
                                        onActionDown(view)
                                    }
                                }
                            }

                            EMotor.MOTOR6.key -> {
                                when (viewTag) {
                                    EMotorBtn.UP.value -> {
                                        view.code = MOTOR6_UP
                                        view.img = context.getString(R.string.bed_all)
                                        view.pressType = context.getString(R.string.press_keep)
                                        onActionDown(view)
                                    }
                                    EMotorBtn.DOWN.value -> {
                                        view.code = MOTOR6_DOWN
                                        view.img = context.getString(R.string.bed_all)
                                        view.pressType = context.getString(R.string.press_keep)
                                        onActionDown(view)
                                    }
                                }
                            }

                            EMotor.HEAD_FOOT_BOTH.key -> {
                                when (viewTag) {
                                    EMotorBtn.UP.value -> {
                                        view.code = HEAD_FOOT_BOTH_UP
                                        view.img = context.getString(R.string.bed_head_foot_both)
                                        view.pressType = context.getString(R.string.press_keep)
                                        onActionDown(view)
                                    }
                                    EMotorBtn.DOWN.value -> {
                                        view.code = HEAD_FOOT_BOTH_DOWN
                                        view.img = context.getString(R.string.bed_head_foot_both)
                                        view.pressType = context.getString(R.string.press_keep)
                                        onActionDown(view)
                                    }
                                }
                            }

                            EMotor.PILLOW_LUMBAR_BOTH.key -> {
                                when (viewTag) {
                                    EMotorBtn.UP.value -> {
                                        view.code = PILLOW_LUMBAR_BOTH_UP
                                        view.img =
                                                context.getString(R.string.bed_pillow_lumbar_both)
                                        view.pressType = context.getString(R.string.press_keep)
                                        onActionDown(view)
                                    }
                                    EMotorBtn.DOWN.value -> {
                                        view.code = PILLOW_LUMBAR_BOTH_DOWN
                                        view.img =
                                                context.getString(R.string.bed_pillow_lumbar_both)
                                        view.pressType = context.getString(R.string.press_keep)
                                        onActionDown(view)
                                    }
                                }
                            }

                            EMotor.PILLOW_LUMBAR_BOTH_TILT.key -> {
                                when (viewTag) {
                                    EMotorBtn.UP.value -> {
                                        view.code = PILLOW_LUMBAR_BOTH_TILT_UP
                                        view.img =
                                                context.getString(R.string.bed_pillow_lumbar_both)
                                        view.pressType = context.getString(R.string.press_keep)
                                        onActionDown(view)
                                    }
                                    EMotorBtn.DOWN.value -> {
                                        view.code = PILLOW_LUMBAR_BOTH_TILT_DOWN
                                        view.img =
                                                context.getString(R.string.bed_pillow_lumbar_both)
                                        view.pressType = context.getString(R.string.press_keep)
                                        onActionDown(view)
                                    }
                                }
                            }

//                            EMotor.HEAD_UP_FOOT_UP.key -> {
//                                when (viewTag) {
//                                    EMotorBtn.UP.value -> {
//                                        view.code = HEAD_UP_FOOT_UP
//                                        view.img = context.getString(R.string.bed_all)
//                                        view.pressType = context.getString(R.string.press_once)
//                                        onActionDown(view)
//                                    }
//                                }
//                            }
//
//                            EMotor.HEAD_UP_FOOT_DOWN.key -> {
//                                when (viewTag) {
//                                    EMotorBtn.UP.value -> {
//                                        view.code = HEAD_UP_FOOT_DOWN
//                                        view.img = context.getString(R.string.bed_all)
//                                        view.pressType = context.getString(R.string.press_once)
//                                        onActionDown(view)
//                                    }
//                                }
//                            }
//
//                            EMotor.HEAD_DOWN_FOOT_UP.key -> {
//                                when (viewTag) {
//                                    EMotorBtn.UP.value -> {
//                                        view.code = HEAD_DOWN_FOOT_UP
//                                        view.img = context.getString(R.string.bed_all)
//                                        view.pressType = context.getString(R.string.press_once)
//                                        onActionDown(view)
//                                    }
//                                }
//                            }
//
//                            EMotor.HEAD_UP_ONLY.key -> {
//                                when (viewTag) {
//                                    EMotorBtn.UP.value -> {
//                                        view.code = HEAD_UP_ONLY
//                                        view.img = context.getString(R.string.head_up)
//                                        view.pressType = context.getString(R.string.press_once)
//                                        onActionDown(view)
//                                    }
//                                }
//                            }
//
//                            EMotor.FOOT_UP_ONLY.key -> {
//                                when (viewTag) {
//                                    EMotorBtn.UP.value -> {
//                                        view.code = FOOT_UP_ONLY
//                                        view.img = context.getString(R.string.foot_up)
//                                        view.pressType = context.getString(R.string.press_once)
//                                        onActionDown(view)
//                                    }
//                                }
//                            }

                        }
                    }

                    MotionEvent.ACTION_MOVE -> {
                        if (inRangeOfView(view, event.rawX.toInt(), event.rawY.toInt())) {
                            onActionUp(view)
                        }
                    }

                    MotionEvent.ACTION_CANCEL,
                    MotionEvent.ACTION_UP -> {
                        onActionUp(view)
                    }
                }
            }
            return false
        }
    }

    private fun setTabIconN(tab: TabLayout.Tab, itemCode: Int) {
        tab.customView?.findViewById<TextView>(R.id.tvTitle)?.textColor = whiteColor
        when (itemCode) {
            EMotor.HEAD.key -> {
                tab.customView?.findViewById<ImageView>(R.id.ivIcon)?.setImageResource(R.drawable.ic_head_n)
                showMotorButton(EMotorBtn.BOTH.key)
            }
            EMotor.FOOT.key -> {
                tab.customView?.findViewById<ImageView>(R.id.ivIcon)?.setImageResource(R.drawable.ic_foot_n)
                showMotorButton(EMotorBtn.BOTH.key)
            }
            EMotor.PILLOW.key -> {
                tab.customView?.findViewById<ImageView>(R.id.ivIcon)?.setImageResource(R.drawable.ic_pillow_n)
                showMotorButton(EMotorBtn.BOTH.key)
            }
            EMotor.LUMBAR.key -> {
                tab.customView?.findViewById<ImageView>(R.id.ivIcon)?.setImageResource(R.drawable.ic_lumbar_n)
                showMotorButton(EMotorBtn.BOTH.key)
            }
            EMotor.MOTOR5.key -> {
                tab.customView?.findViewById<ImageView>(R.id.ivIcon)?.setImageResource(R.drawable.ic_motor5)
                showMotorButton(EMotorBtn.BOTH.key)
            }
            EMotor.MOTOR6.key -> {
                tab.customView?.findViewById<ImageView>(R.id.ivIcon)?.setImageResource(R.drawable.ic_motor6)
                showMotorButton(EMotorBtn.BOTH.key)
            }
            EMotor.HEAD_FOOT_BOTH.key -> {
                tab.customView?.findViewById<ImageView>(R.id.ivIcon)?.setImageResource(R.drawable.ic_head_foot_both)
                showMotorButton(EMotorBtn.BOTH.key)
            }
            EMotor.PILLOW_LUMBAR_BOTH.key -> {
                tab.customView?.findViewById<ImageView>(R.id.ivIcon)?.setImageResource(R.drawable.ic_pillow_lumbar_both)
                showMotorButton(EMotorBtn.BOTH.key)
            }
            EMotor.PILLOW_LUMBAR_BOTH_TILT.key -> {
                tab.customView?.findViewById<ImageView>(R.id.ivIcon)?.setImageResource(R.drawable.ic_pillow_lumbar_both_tilt)
                showMotorButton(EMotorBtn.BOTH.key)
            }
//            EMotor.HEAD_UP_FOOT_UP.key -> {
//                tab.customView?.findViewById<ImageView>(R.id.ivIcon)?.setImageResource(R.drawable.ic_head_up_foot_up)
//                showMotorButton(EMotorBtn.UP.key)
//            }
//            EMotor.HEAD_UP_FOOT_DOWN.key -> {
//                tab.customView?.findViewById<ImageView>(R.id.ivIcon)?.setImageResource(R.drawable.ic_head_up_foot_down)
//                showMotorButton(EMotorBtn.UP.key)
//            }
//            EMotor.HEAD_DOWN_FOOT_UP.key -> {
//                tab.customView?.findViewById<ImageView>(R.id.ivIcon)?.setImageResource(R.drawable.ic_head_down_foot_up)
//                showMotorButton(EMotorBtn.UP.key)
//            }
//            EMotor.HEAD_UP_ONLY.key -> {
//                tab.customView?.findViewById<ImageView>(R.id.ivIcon)?.setImageResource(R.drawable.ic_head_up_only_n)
//                showMotorButton(EMotorBtn.UP.key)
//            }
//            EMotor.FOOT_UP_ONLY.key -> {
//                tab.customView?.findViewById<ImageView>(R.id.ivIcon)?.setImageResource(R.drawable.ic_foot_up_only_n)
//                showMotorButton(EMotorBtn.UP.key)
//            }
        }
    }

    private fun setTabIconP(tab: TabLayout.Tab, itemCode: Int) {
        tab.customView?.findViewById<TextView>(R.id.tvTitle)?.textColor = goldenColor
        when (itemCode) {
            EMotor.HEAD.key -> {
                tab.customView?.findViewById<ImageView>(R.id.ivIcon)?.setImageResource(R.drawable.ic_head_p)
                showMotorButton(EMotorBtn.BOTH.key)
            }
            EMotor.FOOT.key -> {
                tab.customView?.findViewById<ImageView>(R.id.ivIcon)?.setImageResource(R.drawable.ic_foot_p)
                showMotorButton(EMotorBtn.BOTH.key)
            }
            EMotor.PILLOW.key -> {
                tab.customView?.findViewById<ImageView>(R.id.ivIcon)?.setImageResource(R.drawable.ic_pillow_p)
                showMotorButton(EMotorBtn.BOTH.key)
            }
            EMotor.LUMBAR.key -> {
                tab.customView?.findViewById<ImageView>(R.id.ivIcon)?.setImageResource(R.drawable.ic_lumbar_p)
                showMotorButton(EMotorBtn.BOTH.key)
            }
            EMotor.MOTOR5.key -> {
                tab.customView?.findViewById<ImageView>(R.id.ivIcon)?.setImageResource(R.drawable.ic_motor5)
                showMotorButton(EMotorBtn.BOTH.key)
            }
            EMotor.MOTOR6.key -> {
                tab.customView?.findViewById<ImageView>(R.id.ivIcon)?.setImageResource(R.drawable.ic_motor6)
                showMotorButton(EMotorBtn.BOTH.key)
            }
            EMotor.HEAD_FOOT_BOTH.key -> {
                tab.customView?.findViewById<ImageView>(R.id.ivIcon)?.setImageResource(R.drawable.ic_head_foot_both)
                showMotorButton(EMotorBtn.BOTH.key)
            }
            EMotor.PILLOW_LUMBAR_BOTH.key -> {
                tab.customView?.findViewById<ImageView>(R.id.ivIcon)?.setImageResource(R.drawable.ic_pillow_lumbar_both)
                showMotorButton(EMotorBtn.BOTH.key)
            }
            EMotor.PILLOW_LUMBAR_BOTH_TILT.key -> {
                tab.customView?.findViewById<ImageView>(R.id.ivIcon)?.setImageResource(R.drawable.ic_pillow_lumbar_both_tilt)
                showMotorButton(EMotorBtn.BOTH.key)
            }
//            EMotor.HEAD_UP_FOOT_UP.key -> {
//                tab.customView?.findViewById<ImageView>(R.id.ivIcon)?.setImageResource(R.drawable.ic_head_up_foot_up)
//                showMotorButton(EMotorBtn.UP.key)
//            }
//            EMotor.HEAD_UP_FOOT_DOWN.key -> {
//                tab.customView?.findViewById<ImageView>(R.id.ivIcon)?.setImageResource(R.drawable.ic_head_up_foot_down)
//                showMotorButton(EMotorBtn.UP.key)
//            }
//            EMotor.HEAD_DOWN_FOOT_UP.key -> {
//                tab.customView?.findViewById<ImageView>(R.id.ivIcon)?.setImageResource(R.drawable.ic_head_down_foot_up)
//                showMotorButton(EMotorBtn.UP.key)
//            }
//            EMotor.HEAD_UP_ONLY.key -> {
//                tab.customView?.findViewById<ImageView>(R.id.ivIcon)?.setImageResource(R.drawable.ic_head_up_only_n)
//                showMotorButton(EMotorBtn.UP.key)
//            }
//            EMotor.FOOT_UP_ONLY.key -> {
//                tab.customView?.findViewById<ImageView>(R.id.ivIcon)?.setImageResource(R.drawable.ic_foot_up_only_n)
//                showMotorButton(EMotorBtn.UP.key)
//            }
        }
    }

    val onTabSelectedListener = object : TabLayout.OnTabSelectedListener {
        override fun onTabReselected(tab: TabLayout.Tab?) {
//            LogUtil.e("onTabReselected")
        }

        override fun onTabUnselected(tab: TabLayout.Tab?) {
            val itemCode = tab?.tag
            if (itemCode != null) {
                setTabIconN(tab, itemCode as Int)
            }
        }

        override fun onTabSelected(tab: TabLayout.Tab?) {
//            LogUtil.e("onTabSelected")
            val itemCode = tab?.tag
            if (itemCode != null) {
                setTabIconP(tab, itemCode as Int)
            }
        }
    }


    fun setTabData() {
        ThreadUtils.ui {
            if (motorMap.size > 0) {
                tabLayout.removeAllTabs()


                for (item in motorMap) {

                    when (item.key) {
                        EMotor.HEAD.key -> {
                            addTab(context.getString(R.string.f_head), EMotor.HEAD.key, R.drawable.ic_head_n)
                        }

                        EMotor.FOOT.key -> {
                            addTab(context.getString(R.string.f_foot), EMotor.FOOT.key, R.drawable.ic_foot_n)
                        }

                        EMotor.PILLOW.key -> {
                            addTab(
                                    context.getString(R.string.f_pillow),
                                    EMotor.PILLOW.key,
                                    R.drawable.ic_pillow_n
                            )
                        }

                        EMotor.LUMBAR.key -> {
                            addTab(
                                    context.getString(R.string.f_lumbar),
                                    EMotor.LUMBAR.key,
                                    R.drawable.ic_lumbar_n
                            )
                        }

                        EMotor.MOTOR5.key -> {
                            addTab(
                                    EMotor.MOTOR5.key.toString(),
                                    EMotor.MOTOR5.key,
                                    R.drawable.ic_motor5
                            )
                        }

                        EMotor.MOTOR6.key -> {
                            addTab(
                                    EMotor.MOTOR6.key.toString(),
                                    EMotor.MOTOR6.key,
                                    R.drawable.ic_motor6
                            )
                        }

                        EMotor.HEAD_FOOT_BOTH.key -> {
                            addTab(
                                    EMotor.HEAD_FOOT_BOTH.key.toString(),
                                    EMotor.HEAD_FOOT_BOTH.key,
                                    R.drawable.ic_head_foot_both
                            )
                        }
                        EMotor.PILLOW_LUMBAR_BOTH.key -> {
                            addTab(
                                    EMotor.PILLOW_LUMBAR_BOTH.key.toString(),
                                    EMotor.PILLOW_LUMBAR_BOTH.key,
                                    R.drawable.ic_pillow_lumbar_both
                            )
                        }
                        EMotor.PILLOW_LUMBAR_BOTH_TILT.key -> {
                            addTab(
                                    EMotor.PILLOW_LUMBAR_BOTH_TILT.key.toString(),
                                    EMotor.PILLOW_LUMBAR_BOTH_TILT.key,
                                    R.drawable.ic_pillow_lumbar_both_tilt
                            )
                        }

//                        EMotor.HEAD_UP_FOOT_UP.key -> {
//                            addTab(
//                                    EMotor.HEAD_UP_FOOT_UP.key.toString(),
//                                    EMotor.HEAD_UP_FOOT_UP.key,
//                                    R.drawable.ic_head_up_foot_up
//                            )
//                        }
//
//                        EMotor.HEAD_UP_FOOT_DOWN.key -> {
//                            addTab(
//                                    EMotor.HEAD_UP_FOOT_DOWN.key.toString(),
//                                    EMotor.HEAD_UP_FOOT_DOWN.key,
//                                    R.drawable.ic_head_up_foot_down
//                            )
//                        }
//
//                        EMotor.HEAD_DOWN_FOOT_UP.key -> {
//                            addTab(
//                                    EMotor.HEAD_DOWN_FOOT_UP.key.toString(),
//                                    EMotor.HEAD_DOWN_FOOT_UP.key,
//                                    R.drawable.ic_head_down_foot_up
//                            )
//                        }
//
//                        EMotor.HEAD_UP_ONLY.key -> {
//
//                            addTab(
//                                    EMotor.HEAD_UP_ONLY.key.toString(),
//                                    EMotor.HEAD_UP_ONLY.key,
//                                    R.drawable.ic_head_up_only_n
//                            )
//                        }
//
//                        EMotor.FOOT_UP_ONLY.key -> {
//                            addTab(
//                                    EMotor.FOOT_UP_ONLY.key.toString(),
//                                    EMotor.FOOT_UP_ONLY.key,
//                                    R.drawable.ic_foot_up_only_n
//                            )
//                        }
                    }

                }
            }
        }
    }

    private fun showMotorButton(btnTyp: Int) {
        when (btnTyp) {
            EMotorBtn.BOTH.key -> {
                motorUp.visibility = View.VISIBLE
                motorDown.visibility = View.VISIBLE
            }

            EMotorBtn.UP.key -> {
                motorUp.visibility = View.VISIBLE
                motorDown.visibility = View.GONE
            }

            EMotorBtn.DOWN.key -> {
                motorUp.visibility = View.GONE
                motorDown.visibility = View.VISIBLE
            }
        }
    }

    fun addTab(itemTitle: String, ItemCode: Int, drawableRes: Int) {
        val tab = tabLayout.newTab()
        tab.setText(itemTitle)
        val view: View = LayoutInflater.from(context).inflate(R.layout.layout_tab, null)
        val tvTitle: TextView = view.findViewById(R.id.tvTitle)
        val ivIcon: ImageView = view.findViewById(R.id.ivIcon)
        tvTitle.setText(itemTitle)
        ivIcon.setImageResource(drawableRes)
//        ivIcon.background = ContextCompat.getDrawable(context, drawableRes)
        tab.setCustomView(view)
        tab.tag = ItemCode
        tabLayout.addTab(tab)
    }


    //自定义图标字体和文字
//    fun getTabView(pos: Int): View {
//        //首先为子tab布置一个布局
//        val view: View = LayoutInflater.from(context).inflate(R.layout.layout_tab, null)
//        val tvTitle: TextView = view.findViewById(R.id.tvTitle)
//        val ivIcon: ImageView = view.findViewById(R.id.ivIcon);
//        tvTitle.setText(motorMap.get(pos));
//        tvTitle.setText(motorMap.get(pos));
//        ivIcon.setImageResource(tabIcons.get(pos));
//        return view;
//    }


    override fun initDestroy() {
        EventBus.getDefault().unregister(this)
    }


    @Subscribe(threadMode = ThreadMode.MAIN)
    fun onBedImgEvent(event: BedImgEvent) {
        setBedImg(event.bedStr)
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    fun onFunctionEvent(event: FunChangeEvent) {
        when (event.fragCode) {
            EArea.MOTOR.key -> {
                showFrag()
            }
        }
    }


    //
    @Synchronized
    private fun showFrag() {
        if (motorMap.size > 0) {
            if (rlBtn.visibility.equals(View.INVISIBLE)) {
                rlBtn.visibility = View.VISIBLE
            }

            if (llBtn.visibility.equals(View.INVISIBLE)) {
                llBtn.visibility = View.VISIBLE
            }



            refreshMotorView()
//            LogUtil.e("马达进了")
//            setTabData()
//            motorFragAdapter.setData(motorMap, tabLayout)
//            LogUtil.e("马达出了")
        }
    }

    private fun refreshMotorView() {
        if (cmdList == null) {
            cmdList = ArrayList<String>()
        }
        cmdList?.let {
            cmdList!!.clear()
            for ((_, value) in motorMap) {
                value.forEach {
                    it.code.let { cmdList!!.add(it) }
                }
            }
            rootView?.let { traverseMotorView(it, cmdList!!) }
            setTabData()
        }
    }

    //离线脱机显示按钮控件
    private fun showFunction() {
        if (motorMap.size <= 0) {
            motorMap.put(
                    EMotor.HEAD.key,
                    mutableListOf<AreaValue>(
                            AreaValue(
                                    EArea.MOTOR.key,
                                    HEAD_UP
                            ),
                            AreaValue(
                                    EArea.MOTOR.key,
                                    HEAD_DOWN
                            )
                    )
            )

            refreshMotorView()
//            motorFragAdapter.setData(motorMap, tabLayout)
        }
    }

    private fun setBedImg(bedStr: String) {
        when (bedStr) {
            context.getString(R.string.bed_none) -> {
                ivBed!!.setImageResource(R.drawable.ic_bed_none)
            }
            context.getString(R.string.bed_head) -> {
                ivBed!!.setImageResource(R.drawable.ic_bed_head)
            }
            context.getString(R.string.bed_foot) -> {
                ivBed!!.setImageResource(R.drawable.ic_bed_foot)
            }
            context.getString(R.string.bed_pillow) -> {
                ivBed!!.setImageResource(R.drawable.ic_bed_pillow)
            }
            context.getString(R.string.bed_lumbar) -> {
                ivBed!!.setImageResource(R.drawable.ic_bed_lumbar)
            }
            context.getString(R.string.bed_head_foot_both) -> {
                ivBed!!.setImageResource(R.drawable.ic_bed_head_foot_both)
            }
            context.getString(R.string.bed_pillow_lumbar_both)
            -> {
                ivBed!!.setImageResource(R.drawable.ic_bed_all)
            }
            context.getString(R.string.bed_all)
            -> {
                ivBed!!.setImageResource(R.drawable.ic_bed_all)
            }
            context.getString(R.string.bed_ubl) -> {
                if (ivBed!!.tag == null) {
                    ivBed!!.setImageResource(R.drawable.ic_bed_ubl)
                    ivBed!!.tag = R.drawable.ic_bed_ubl
                } else {
                    ivBed!!.setImageResource(R.drawable.ic_bed_none)
                    ivBed!!.tag = null
                }
            }
        }
    }

}